
import 'base_viewmodel_mvvm.dart';

class LoginViewModel extends BaseViewModel<LoginModel> {

  LoginViewModel();

  String loginName = "";
  String psw = "";

  ///登录
  void login() {
    if (loginName.isEmpty) {
      showToast("登录账号不可为空");
    } else if (psw.isEmpty) {
      showToast("登录密码不可为空");
    } else {
      sendRequest<LoginResult>(model.login(loginName, psw), (value) {
        if (value.errorCode == 0) {
          value.data?.let((it) {
            UserInfoSp.getInstance().uid = it.id ?? 0;
            UserInfoSp.getInstance().token = it.token ?? "";
            UserInfoSp.getInstance().userName = it.username ?? "";
          });
          pop();
          push(MainPage());
        } else {
          showToast(value.errorMsg!);
        }
      }, isNeedLoading: true);
    }
  }
}